from predict_final import identify
# =====================上面代码必须处于第一行！=====================
import sys
import os
from MainWin import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QIcon
from qt_material import apply_stylesheet

# =====================功能模块导入=====================
from file_utils import open_file_dialog
from log2sql import process_log_file
from table import sql2ui
from stats_utils import show_stats_dialog
from export_utils import export_table_to_csv
from advanced_filter_window import show_advanced_filter


sys.path.append(os.path.dirname(os.path.abspath(__file__)))
def setup_ui_elements(window, ui):
    """设置UI元素和事件处理"""
    # 连接按钮点击事件到文件选择对话框
    ui.pushButton.clicked.connect(lambda: handle_file_open(ui))
    ui.pushButton_2.clicked.connect(lambda: aggregated(ui))
    ui.pushButton_3.clicked.connect(lambda: sql2ui(ui))
    ui.actiontongji.triggered.connect(lambda: show_stats_dialog(window))
    ui.action_csv.triggered.connect(lambda: export_table_to_csv(window, ui))
    ui.action.triggered.connect(lambda: show_advanced_filter(window))



def handle_file_open(ui):
    """处理文件打开按钮点击事件"""
    file_path = open_file_dialog(
        parent=ui.pushButton,
        caption="选择日志文件",
        file_filter="日志文件 (*.log *.txt);;文本文件 (*.txt);;所有文件 (*.*)"
    )
    # 如果用户选择了文件，更新文本框显示文件路径
    if file_path:
        ui.lineEdit.setText(file_path)
        # 设置进度条和提示
        ui.label_2.setText("文件已选择")
        ui.progressBar.setValue(1)
        # log2sql
        process_log_file(file_path, ui)
        # 获取 checkBox 和 checkBox_2的选中状态 此处为是否进行AI和正则判断结果
        checkBox = ui.checkBox.isChecked()
        checkBox_2 = ui.checkBox_2.isChecked()

        # 恶意检测
        identify(ui, checkBox, checkBox_2)

        # sql2ui
        sql2ui(ui)
def aggregated(ui):
    """处理聚合按钮点击事件"""
    # sql2ui
    sql2ui(ui, aggregated=True)
    


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(window)
    # 设置UI元素和事件
    setup_ui_elements(window, ui)
    apply_stylesheet(app, theme='light_amber.xml')
    window.setWindowIcon(QIcon("logo.jpg"))
    window.show()

    sys.exit(app.exec_())
